/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.custom;

import com.zhuangfei.timetable.operater.SimpleOperater;

import ohos.agp.components.DirectionalLayout;

/**
 * 自定义的课表业务操作者，以实现列宽设置。
 *
 * @version 2.0.6
 * Created by Liu ZhuangFei on 2018/9/1.
 */
public class CustomOperater extends SimpleOperater {
    /**
     * 宽度权重
     */
    private float[] weights;

    /**
     * CustomOperater
     */
    public CustomOperater() {
        weights = new float[7];
        for (int i = 0; i < weights.length; i++) {
            weights[i] = 1;
        }
    }

    @Override
    public void applyWidthConfig() {
        super.applyWidthConfig();
        if (weights == null) {
            return;
        }
        for (int i = 0; i < panels.length; i++) {
            DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(0,
                    DirectionalLayout.LayoutConfig.MATCH_PARENT,
                    DirectionalLayout.LayoutConfig.UNSPECIFIED_ALIGNMENT, weights[i]);
            panels[i].setLayoutConfig(lp);

        }
    }

    /**
     * 设置宽度
     *
     * @param weight
     */
    public void setWidthWeights(float[] weight) {
        if (weight == null || weight.length < 7) {
            this.weights = null;
        } else {
            this.weights = weight.clone();
        }
    }

    public float[] getWeights() {
        if (this.weights != null) {
            return weights.clone();
        } else {
            return null;
        }
    }
}
